# +_+ coding:utf-8 +_+

__author__ = 'cz'

import json
import config
import request


class Ticker(object):
    last = 0
    buy = 0
    sell = 0
    high = 0
    low = 0
    vol = 0
    contract_id = 0
    unit_amount = 0

    def __init__(self):
        pass


class TickerRequest(request.Request):
    def __init__(self):
        super(TickerRequest, self).__init__()
        self.set_host(config.svr_host)
        self.set_path(config.svr_path + "/future_ticker.do")
        self.set_headers({"Content-type": "application/x-www-form-urlencoded"})
        param = dict()
        param["symbol"] = "btc_usd"
        param["contract_type"] = "quarter"
        param["app_key"] = config.app_key
        self.set_parameters(param)


def query():
    req = TickerRequest()
    rsp = req.do_request()
    body = rsp.body()
    body_json = json.loads(body)
    if body_json.has_key("error_code"):
        return None
    ticker_json = body_json["ticker"]
    ticker = Ticker()
    ticker.buy = ticker_json["buy"]
    ticker.contract_id = ticker_json["contract_id"]
    ticker.high = ticker_json["high"]
    ticker.last = ticker_json["last"]
    ticker.low = ticker_json["low"]
    ticker.sell = ticker_json["sell"]
    ticker.unit_amount = ticker_json["unit_amount"]
    ticker.vol = ticker_json["vol"]

    return ticker
